package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/7/16 15:47
 * @Description:预约设置
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

	@Reference
	OrderSettingService orderSettingService;

	/**
	 *  @描述：上传模板文件
	 */
	@RequestMapping("/upload")
	public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
		try {
			List<String[]> strings = POIUtils.readExcel(excelFile);
			List<OrderSetting> list= new ArrayList<>();
			if(!CollectionUtils.isEmpty(strings)){
				strings.forEach(item->{
					//	将String数组转换成OrderSetting对象
					OrderSetting orderSetting = new OrderSetting();
					//LocalDate
					orderSetting.setOrderDate(new Date(item[0]));
					orderSetting.setNumber(Integer.parseInt(item[1]));
					list.add(orderSetting);
				});
			}
			orderSettingService.upload(list);

		} catch (Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
		}

	  return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
	}
	/**
	 *  @描述：获取预约设置数据
	 */
	@RequestMapping("/getOrdersettingData")
	public Result getOrdersettingData(String month) {
		try {
			List<Map> result = orderSettingService.getOrdersettingData(month);
			return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,result);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
		}
	}
	
	/**
	 *  @描述：根据日期设置可预约人数
	 */
	@RequestMapping("/setNumberByOrderDate")
	public Result setNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
		try {
			orderSettingService.setNumberByOrderDate(orderSetting);
		} catch (Exception e){
			e.printStackTrace();
			return new Result(false,MessageConstant.ORDERSETTING_FAIL);


		}
	  return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
	}
}
